#include "stk_message_http.h"
#include <string.h>

using namespace stk;

int main()
{
    const char *str_http =
        "POST /Request_Resource HTTP/1.1\r\n"
        "Content-Type: application/xml\r\n"
        "Connection: Keep-Alive, 123\r\n"
        "Host: localhost, 321\r\n"
        "Public: OPTIONS, MGET, MHEAD, GET, HEAD\r\n"
        "Content-Length: 185\r\n\r\n"
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
        "<HTTP_XML EventType=\"Request_Resource\">\r\n"
        "<Item Code=\"100410000000000000\" UserCode=\"100410000004000000\" FromIndex=\"1\" ToIndex=\"100\"/>\r\n"
        "</HTTP_XML>\r\n\r\n";

    HttpMsg Msg(str_http, strlen(str_http));

    string strMsg = "";
    Msg.to_str(strMsg);

    string strContentType = Msg.GetContentType();
    string strConnection  = Msg.GetConnection();
    int    content_lengh  = Msg.GetContentLength_int();
    string strBody        = Msg.GetBody();

    HttpMsg msg4 = Msg;
    strContentType = msg4.GetContentType();
    strConnection  = msg4.GetConnection();
    content_lengh  = msg4.GetContentLength_int();


    Msg.SetContentType("application/ocx");
    Msg.SetContentLength(123);

    strContentType = Msg.GetContentType();
    content_lengh  = Msg.GetContentLength_int();

    HttpMsg msg1;

    ContentType content_type("application/xml");
    Connection  connecion;
    connecion.parse("Keep-Alive", strlen("Keep-Alive"));

    strContentType = "";
    content_type.to_str(strContentType);

    strConnection = "";
    connecion.to_str(strConnection);

    //		msg1.SetContentType(strContentType.c_str());
    msg1.SetContentType(content_type);

    msg1.SetConnection(strConnection.c_str());

    msg1.SetMethod("POST");
//	msg1.SetUrl("/heartbeat");
    msg1.SetVersion("HTTP/1.1");
    msg1.SetBody(Msg.GetBody().c_str());

    strMsg = "";
    msg1.to_str(strMsg);

    HttpMsg msg2 = msg1;
    strMsg = "";
    msg2.to_str(strMsg);

    const char *req = \
                      "POST /test HTTP/1.1\r\n"
                      "Content-Type: application/json\r\n"
                      "Connection: Keep-Alive\r\n";

    HttpMsg httpReq(req, strlen(req));
    httpReq.SetOrigin("null");

    string str_req;
    httpReq.to_str(str_req);

    HttpMsg parserReq(str_req.c_str(), (int32_t)str_req.size());
    string orgin = parserReq.GetOrigin();

    const char *rsp = \
                      "HTTP/1.1 200 OK\r\n"
                      "Allow: GET, PUT\r\n"
                      "Public: OPTIONS, MGET, MHEAD, GET, HEAD\r\n";
    HttpMsg httpRsp(rsp, strlen(rsp));
    httpRsp.SetCorsOrigin("*");
    httpRsp.SetCorsHeader("AuthToken");
    httpRsp.SetCorsHeader("Authorization");
    httpRsp.SetCorsHeader("Origin");
    httpRsp.SetCorsHeader("Content-Type");
    httpRsp.SetCorsHeader("Accept");
    httpRsp.SetCorsHeader("X-Requested-With");
    httpRsp.SetCorsMethod("GET");
    httpRsp.SetCorsMethod("POST");
    httpRsp.SetCorsMethod("PUT");
    httpRsp.SetCorsMethod("DELETE");
    httpRsp.SetCorsMethod("OPTIONS");

    httpRsp.SetContentEncoding("gzip");
    httpRsp.SetAcceptEncoding("gzip");
    httpRsp.SetAcceptEncoding("deflate");
    httpRsp.SetAcceptEncoding("br");

    string str_rsp;
    httpRsp.to_str(str_rsp);

    HttpMsg parserRsp(str_rsp.c_str(), (int32_t)str_rsp.size());

    const char* websock = \
                          "GET / HTTP/1.1                                                                                                                 \r\n"
                          "Host: 127.0.0.1:8896                                                                                                           \r\n"
                          "Connection: Upgrade                                                                                                            \r\n"
                          "Pragma: no-cache                                                                                                               \r\n"
                          "Cache-Control: no-cache                                                                                                        \r\n"
                          "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36\r\n"
                          "Upgrade: websocket                                                                                                             \r\n"
                          "Origin: file://                                                                                                                \r\n"
                          "Sec-WebSocket-Version: 13                                                                                                      \r\n"
                          "Accept-Encoding: gzip, deflate, br                                                                                             \r\n"
                          "Content-Encoding: gzip                                                                                                         \r\n"
                          "Accept-Language: zh-CN,zh;q=0.9                                                                                                \r\n"
                          "Sec-WebSocket-Key: 83EQijtiqt8kq+swsG+S4g==                                                                                    \r\n"
                          "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits                                                           \r\n";

    HttpMsg websockMsg(websock, (int32_t)strlen(websock));
    string websock_key = websockMsg.GetWebsockKey();
    string websock_ver = websockMsg.GetWebsockVersion();
    string websock_ext = websockMsg.GetWebsockExtensions();
    string upgrade     = websockMsg.GetUpgrade();
    string accept_enco = websockMsg.GetAcceptEncoding();
    string content_enc = websockMsg.GetContentEncoding();

    string websock_ori = websockMsg.GetOrigin();

    const char* websock_rsp = \
                              "HTTP/1.1 101 Switching Protocols\r\n"
                              "Server: caiwang213 websocket server\r\n"
                              "Upgrade: WebSocket\r\n"
                              "Connection: Upgrade\r\n"
                              "\r\n";
    HttpMsg websockRspMsg(websock_rsp, (int32_t)strlen(websock_rsp));
    websockRspMsg.SetWebsockAccept("83EQijtiqt8kq+swsG+S4g==");

    string str_websock_rsp;
    websockRspMsg.to_str(str_websock_rsp);

    return 0;
}
